// pages/appointment/appointment.js
import * as API_Appointment from '../../api/appointment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subState: false,
    appointment: {
      storeId: '',
      appointmentDate: '',
      appointmentItem: '',
      appointmentNum: 1,
      timeFrame: '',
      technicianId: '',
      appointmentRemark: '',
      appointmentPhone: ''
    },
    serviceList: [],
    dateList: [],
    timeList: [],
    jishiList: [],
  },
  numsub: function () {
    let appointmentNum = this.data.appointment.appointmentNum;
    if (appointmentNum > 1) {
      appointmentNum = appointmentNum - 1;
    }
    this.setData({ 'appointment.appointmentNum': appointmentNum });
  },
  numPlus: function () {
    let appointmentNum = this.data.appointment.appointmentNum;
    if (appointmentNum < 10) {
      appointmentNum = appointmentNum + 1;
    }
    this.setData({ 'appointment.appointmentNum': appointmentNum });
  },
  subAppointment: function () {
    let appointment = this.data.appointment;
    // debugger
    this.setData({ subState: true });
    if (!appointment.appointmentItem) {
      wx.showToast({ title: '请选择预约项目', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }
    if (!appointment.appointmentDate) {
      wx.showToast({ title: '请选择预约日期', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }
    // let tteme=!appointment.technicianId;
    // let tteme1=appointment.technicianId!==0;
    // let tteme2= appointment.technicianId===0
    if (!appointment.technicianId && !(appointment.technicianId === 0)) {
      wx.showToast({ title: '请选择预约技师', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }
    if (!appointment.timeFrame) {
      wx.showToast({ title: '请选择预约时段', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }


    if (!appointment.appointmentPhone) {
      wx.showToast({ title: '请填写预约人电话', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }
    if (11 != appointment.appointmentPhone.length) {
      wx.showToast({ title: '请填写正确电话', icon: 'none', duration: 1500 })
      this.setData({ subState: false });
      return;
    }
    API_Appointment.appointment(appointment).then(res => {
      wx.showToast({ title: res.msg, icon: 'none', duration: 1500 })
      if (res.code == 200) {
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/appointmentRecord/appointmentRecord',
          })
        }, 1500);
      } else {
        this.setData({ subState: false });
      }
    })
  },
  phoneChange: function (e) {

    let appointmentPhone = e.detail.value;
    this.setData({ 'appointment.appointmentPhone': appointmentPhone })

  },
  remarkChange: function (e) {

    let appointmentRemark = e.detail.value;
    this.setData({ 'appointment.appointmentRemark': appointmentRemark })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let storeId = options.storeId;
    this.setData({ 'appointment.storeId': storeId });
    let params = { storeId: storeId };
    API_Appointment.baseInfo(params).then(res => {
      let jso = res.data;
      let serviceList = jso.classifyList;
      let dateList = jso.daysArr;
      this.setData({ serviceList: serviceList, dateList: dateList });
      this.selectServiceIndex(0);
      this.selectWeekIndex(0);
    })
  },
  selectService: function (e) {
    let index = e.currentTarget.dataset.index;
    this.selectServiceIndex(index);
  },
  selectServiceIndex: function (index) {
    let serviceList = this.data.serviceList;
    let indexfor = 0;
    serviceList.forEach(item => {
      if (index == indexfor) {
        item.act = true;
        this.setData({ 'appointment.appointmentItem': item.name });
      } else {
        item.act = false;
      }
      indexfor++;
    })
    this.setData({ serviceList: serviceList });
  },

  selectTech: function (e) {
    let index = e.currentTarget.dataset.index;
    this.selectTechIndex(index);
  },
  selectTechIndex: function (index) {
    let jishiList = this.data.jishiList;
    let select = jishiList[index];
    if (!select.canSelect) {
      return;
    }
    this.setData({ 'appointment.timeFrame': '' })
    let indexfor = 0;
    jishiList.forEach(item => {
      if (index == indexfor) {
        item.act = true;
        this.setData({ 'appointment.technicianId': item.id });
      } else {
        item.act = false;
      }
      indexfor++;
    })
    this.setData({ jishiList: jishiList });
    this.listCanAppointmentTimeFrame();
  },
  selectWeek: function (e) {
    let index = e.currentTarget.dataset.index;
    this.selectWeekIndex(index);
  },
  selectWeekIndex: function (index) {
    this.setData({ 'appointment.timeFrame': '', timeList: [] })
    let dateList = this.data.dateList;

    let indexfor = 0;
    dateList.forEach(item => {
      if (index == indexfor) {
        item.act = true;

        this.setData({ 'appointment.appointmentDate': item.appointmentDate });
      } else {
        item.act = false;
      }
      indexfor++;
    })
    this.setData({ dateList: dateList });
    this.listCanAppointmentTechnician();
  },

  selectTime: function (e) {
    let index = e.currentTarget.dataset.index;
    this.selectTimeIndex(index);
  },
  selectTimeIndex: function (index) {
    let timeList = this.data.timeList;


    let indexfor = 0;
    timeList.forEach(item => {
      if (index == indexfor) {
        item.act = true;

        this.setData({ 'appointment.timeFrame': item.name });
      } else {
        item.act = false;
      }
      indexfor++;
    })
    this.setData({ timeList: timeList });
  },
  listCanAppointmentTechnician: function () {
    let storeId = this.data.appointment.storeId;
    let appointmentDate = this.data.appointment.appointmentDate;
    let params = { storeId: storeId, appointmentDate: appointmentDate };
    API_Appointment.listCanAppointmentTechnician(params).then(res => {
      let jso = res.data;
      let jishiList = jso.jsoCan;
      this.setData({ jishiList: jishiList });
      if (jishiList.length > 0) {
        //  debugger
        let technicianId = this.data.appointment.technicianId;//之前选中的技师
        if (!technicianId) {
          //  之前没有选中技师
          this.setData({ 'appointment.timeFrame': '', 'appointment.technicianId': '' })
          this.selectTechIndex(0);
        } else {
          // 之前有选择技师
          let index = 0;
          let flushIndex = 0;
          jishiList.forEach(item => {
            if (item.id == technicianId) {
              if (item.canSelect) {
                //依然可以选择
                flushIndex = index;
              }
            }
            index++;
          })
          this.selectTechIndex(flushIndex);
        }

      }

    })
  },
  listCanAppointmentTimeFrame: function () {

    let storeId = this.data.appointment.storeId;
    let technicianId = this.data.appointment.technicianId;
    let appointmentDate = this.data.appointment.appointmentDate;
    let params = { storeId: storeId, technicianId: technicianId, appointmentDate: appointmentDate };
    API_Appointment.listCanAppointmentTimeFrame(params).then(res => {
      let jso = res.data;
      let timeList = jso.timeList;
      this.setData({ timeList: timeList })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})